package org.itboys.weixin.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mongodb.utils.page.Page;
import org.itboys.weixin.entity.WeixinSendRecord;
import org.itboys.weixin.service.WeixinSendRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping(value = "/weixin/sendrecord")
public class WeixinSendRecordController extends BaseController {

	@Autowired
	private WeixinSendRecordService weixinSendRecordService;

	@RequestMapping("/vm/{weixinSendMessageId}")
	public ModelAndView projectMenus(
			@PathVariable("weixinSendMessageId") Long weixinSendMessageId,
			HttpServletRequest request, Model model) {
		model.addAttribute("weixinSendMessageId", weixinSendMessageId);
		return new ModelAndView("/weixin/sendrecord/list");
	}

	@RequestMapping("/list")
	public void list(HttpServletResponse response, HttpServletRequest request) {
		Page<WeixinSendRecord> page = weixinSendRecordService
				.pageQuery(request);
		JsonPageUtils.renderJsonPage(page.getTotal(), page.getData(), response);
	}

	@RequestMapping("/delete")
	public void delete(HttpServletResponse response, HttpServletRequest request) {
		try {
			Long id = Long.valueOf(request.getParameter("id"));
			weixinSendRecordService.doDelete(id);
			AjaxUtils.renderText(response,
					String.valueOf(1));
		} catch (Exception e) {
			logger.error("weixinsendrecord delete error", e);
			AjaxUtils.renderText(response, "0");
		}
	}
}
